package com.tzxscu.hosdroolsnew.other;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadConfig {
    @Bean(name="pool1")
    public Executor createExecutor(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(1);//核心线程数（默认线程数）
        threadPoolTaskExecutor.setMaxPoolSize(5);//最大线程数
        threadPoolTaskExecutor.setKeepAliveSeconds(60);//允许线程空闲时间（单位：默认为秒）
        threadPoolTaskExecutor.setQueueCapacity(200);//缓冲队列数
        threadPoolTaskExecutor.setThreadNamePrefix("Thread-0");//线程池名前缀
        //线程池对拒绝任务的处理策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return threadPoolTaskExecutor;
    }


    @Bean(name = "pool2")
    public ThreadPoolTaskExecutor createExecutor2(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(1);//核心线程数（默认线程数）
        threadPoolTaskExecutor.setCorePoolSize(5);//最大线程数
        threadPoolTaskExecutor.setKeepAliveSeconds(60);//允许线程空闲时间(单位：默认为秒）
        threadPoolTaskExecutor.setQueueCapacity(200);//缓冲队列数
        threadPoolTaskExecutor.setThreadNamePrefix("Thread-1");//线程池名前缀
        //线程池对拒绝任务的处理策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return threadPoolTaskExecutor;
    }
}
